import { useAppDispatch, useAppSelector } from '@/store'
import { setAudioNextIndex } from '@/store/modules/audio'

export const useChangeAudio = () => {
  // 获得仓库数据
  const { playSongIndex, playSongList } = useAppSelector((state) => ({
    playSongIndex: state.audio.playSongIndex,
    playSongList: state.audio.playSongList
  }))

  const dispatch = useAppDispatch()

  // 播放下一首
  const prevAudio = () => {
    if (playSongIndex === 0) {
      dispatch(setAudioNextIndex(playSongList.length - 1))
    } else {
      dispatch(setAudioNextIndex(playSongIndex - 1))
    }
  }

  // 播放上一首
  const nextAudio = () => {
    if (playSongIndex === playSongList.length - 1) {
      dispatch(setAudioNextIndex(0))
    } else {
      dispatch(setAudioNextIndex(playSongIndex + 1))
    }
  }

  return {
    prevAudio,
    nextAudio
  }
}
